Imports Microsoft.Vbe.Interop

''' <summary>
''' Represents operations that can be made on a Visual Basic Extensibility object
''' </summary>
''' <remarks></remarks>
Public Class VBProjectOperations

    ' The VB Project we're acting on
    Private _ProjectProvider As IVBProjectProvider
    Private _Project As VBProject

#Region "Constructors"

    ''' <summary>
    ''' Default constructor
    ''' </summary>
    ''' <param name="vbProjectProvider">The project provider we're working with</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal vbProjectProvider As IVBProjectProvider)

        ' Check arguments
        If vbProjectProvider Is Nothing Then

            Throw New ArgumentNullException("vbProjectProvider")

        End If


        ' Record the values
        _ProjectProvider = vbProjectProvider
        _Project = _ProjectProvider.GetVBProject()

        ' Make sure we have a reference to the VB project
        If _Project Is Nothing Then

            Throw New Exception("Unable to get reference to the VBA project")

        End If


    End Sub

#End Region

#Region "Decompliation"

    ''' <summary>
    ''' Decompiles The VBA Project to the destination path
    ''' </summary>
    ''' <param name="destPath">The folder to write the VBA files to.</param>
    ''' <remarks></remarks>
    Public Sub Decompile(ByVal destPath As String)

        ' Local Variables
        Dim strFileName As String
    
        ' Make sure the destination directory exists
        If Not IO.Directory.Exists(destPath) Then

            Throw New IO.DirectoryNotFoundException(String.Format("Cannot find output path {0}", destPath))

            ' TODO: Also check directory security

        End If

        ' Loop around and export all the components
        For Each component As VBComponent In _Project.VBComponents

            ' Get the filename
            strFileName = IO.Path.Combine(destPath, component.Name)
            strFileName = IO.Path.ChangeExtension(strFileName, _ProjectProvider.GetComponentFileExtension(component))

            ' Export the file
            Try

                component.Export(strFileName)

            Catch ex As Exception

                Throw New Exception(String.Format("Unable to export component '{0}' to '{1}'", component.Name, strFileName), ex)

            End Try

        Next component

    End Sub


#End Region



End Class



